package com.yuexunit.finance.system.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.system.entity.UserCardTypeInfoEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

/**
 * @author wenwenlong
 */
@Implements(UserCardTypeService.class)
public class UserCardTypeServiceImpl implements UserCardTypeService {

	@Override
	@Transactional
	public Long createUserCardType(UserCardTypeInfoEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		PersistUtil.insert(entity);
		return entity.getCardTypeId();
	}

	@Override
	@Transactional
	public void updateUserCardType(UserCardTypeInfoEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void disableUserCardType(Long cardTypeId) {
		UserCardTypeInfoEntity entity = inquireCardTypeDetail(cardTypeId);
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.FALSE);
		PersistUtil.update(entity);
	}
	
	@Override
	@Transactional
	public void enableUserCardType(Long cardTypeId) {
		UserCardTypeInfoEntity entity = inquireCardTypeDetail(cardTypeId);
		entity.setUpdateDateTime(DateUtil.now());
		entity.setEnabled(Boolean.TRUE);
		PersistUtil.update(entity);
	}

	@Override
	public UserCardTypeInfoEntity inquireCardTypeDetail(Long cardTypeId) {
		return PersistUtil.get(UserCardTypeInfoEntity.class, cardTypeId);
	}

	@Override
	public PageList<UserCardTypeInfoEntity> inquireCardTypePageList(PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserCardTypeInfoEntity.class);
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<UserCardTypeInfoEntity> inquireCardTypeList() {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserCardTypeInfoEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		return PersistUtil.get(criteria);
	}
}
